package com.ihd.ihardware.view.tzc.me.view;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.CompoundButton;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.google.gson.Gson;
import com.ihd.ihardware.R;
import com.ihd.ihardware.common.AppConstans;
import com.ihd.ihardware.databinding.MeFragmentBinding;
import com.ihd.ihardware.pojo.UserInfoRes;
import com.ihd.ihardware.view.enter.view.HomeActivity;
import com.ihd.ihardware.view.enter.view.LoginActivity;
import com.ihd.ihardware.view.enter.view.PerActivity;
import com.ihd.ihardware.view.tzc.main.view.BalanceActivity;
import com.ihd.ihardware.view.tzc.main.viewmodel.BalanceViewModel;

import cn.wowjoy.commonlibrary.base.BaseConsumer;
import cn.wowjoy.commonlibrary.base.BaseFragment;
import cn.wowjoy.commonlibrary.utils.AppActivityManager;
import cn.wowjoy.commonlibrary.utils.DialogUtils;
import cn.wowjoy.commonlibrary.utils.SPUtils;
import cn.wowjoy.commonlibrary.utils.ToastUtils;
import cn.wowjoy.commonlibrary.widget.titlebar.TitleBar;

public class MeFragment extends BaseFragment<MeFragmentBinding, BalanceViewModel> {
    private boolean isVisible = false;
    private UserInfoRes.DataBean ud;

    public MeFragment() {
    }

    public static MeFragment newInstance(String param1, String param2) {
        MeFragment fragment = new MeFragment();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
        }
    }

    @Override
    protected void onCreateViewLazy(Bundle savedInstanceState) {
        if (isVisible)
            ((BalanceActivity) getActivity()).mImmersionBar.statusBarDarkFont(false).init();

        binding.mtitlebar.setTitle("个人中心");
        binding.mtitlebar.setTitleColor(getResources().getColor(R.color.C_FFFFFF));
        binding.mtitlebar.addAction(new TitleBar.Action() {
            @Override
            public String getText() {
                return null;
            }

            @Override
            public int getDrawable() {
                return R.drawable.text_homenb_set;
            }

            @Override
            public void performAction(View view) {
                Intent i = new Intent(getContext(), SettingActivity.class);
                startActivity(i);
            }
        });

        binding.infoRL.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(getContext(), InfoActivity.class);
                startActivity(i);
            }
        });

        binding.membersLL.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(getContext(), MemberActivity.class);
                startActivity(i);
            }
        });

        binding.documentLL.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(getContext(), DocumentActivity.class);
                startActivity(i);
            }
        });

        binding.devices.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(getContext(), HomeActivity.class);
                i.putExtra("userinfo", ud);
                startActivity(i);
            }
        });

        binding.dtTag.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(getContext(), MyPageActivity.class);
                startActivity(i);
            }
        });

        binding.fsTag.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(getContext(), FansActivity.class);
                startActivity(i);
            }
        });

        binding.gzTag.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(getContext(), ConcernActivity.class);
                startActivity(i);
            }
        });

        binding.scTag.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(getContext(), CollectActivity.class);
                startActivity(i);
            }
        });

        binding.targetWeightLL.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!TextUtils.isEmpty(ud.getWeight())) {
                    Intent i = new Intent(getContext(), TargetWeightActivity.class);
                    startActivity(i);
                } else {
                    ToastUtils.showShort(getActivity(), "请先称重或设置当前体重");
                }
            }
        });

        binding.targetStepLL.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(getContext(), TargetStepActivity.class);
                startActivity(i);
            }
        });

        if (SPUtils.getBoolean(AppConstans.VOICE, true))
            binding.voiceBtn.setCheckedNoEvent(true);
        else
            binding.voiceBtn.setCheckedNoEvent(false);

        binding.voiceBtn.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    SPUtils.putBoolean(AppConstans.VOICE, true);
                } else {
                    SPUtils.putBoolean(AppConstans.VOICE, false);
                }
            }
        });

        setRx(AppConstans.INFO, new BaseConsumer<UserInfoRes>() {
            @Override
            protected void onStart() {
            }

            @Override
            protected void onSuccess(UserInfoRes mLiveData) {
                SPUtils.putString(AppConstans.USER_INFO, new Gson().toJson(mLiveData.getData(), UserInfoRes.DataBean.class));
                ud = mLiveData.getData();
                if (!TextUtils.isEmpty(ud.getAvatar()))
                    Glide.with(getActivity())
                            .load(Uri.parse(ud.getAvatar()))
                            .apply(RequestOptions.circleCropTransform())
                            .into(binding.head);
                if (!TextUtils.isEmpty(ud.getNickName()))
                    binding.name.setText(ud.getNickName());
                else
                    binding.name.setText(ud.getUserId());
                binding.dtTV.setText(ud.getSignNum());
                binding.fansTV.setText(ud.getFansNum());
                binding.gzTV.setText(ud.getConcernNum());
                binding.scTV.setText(ud.getCollectNum());
                if (!TextUtils.isEmpty(ud.getDreamWeight()))
                    binding.targetWTV.setText(ud.getDreamWeight() + "kg");
                if (!TextUtils.isEmpty(ud.getDreamStepNumber()))
                    binding.steps.setText(ud.getDreamStepNumber() + "步");
            }

            @Override
            protected void onError(Throwable error) {
                ToastUtils.showShort(getActivity(), error.getMessage());
            }
        });
    }

    @Override
    protected void onResumeLazy() {
        super.onResumeLazy();
        viewModel.getUserInfo();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.me_fragment;
    }

    @Override
    protected Class<BalanceViewModel> getViewModelClass() {
        return BalanceViewModel.class;
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        isVisible = isVisibleToUser;
        if (isVisibleToUser && getActivity() != null) {
            ((BalanceActivity) getActivity()).mImmersionBar.statusBarDarkFont(false).init();
            viewModel.getUserInfo();
        }
    }
}
